﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JY.Common.Extensions;
using JY.Wpf.Core.Tools;

namespace JY.Wpf.Core.Models
{
    /// <summary>
    /// 树节点
    /// </summary>
    public class TreeNode<T> : LocalModel<T>
    {
        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public TreeNode(T sourceData) : base(sourceData)
        {
            this.SubNodes = new ObservableCollection<TreeNode<T>>();
        }

        /// <summary>
        /// 构造器
        /// </summary>
        public TreeNode(Guid id, string name, Guid? parentNodeId, bool isRoot, bool isLeaf, bool? isSelected, bool? isChecked, T sourceData) : this(sourceData)
        {
            this.Id = id;
            this.Name = name;
            this.ParentNodeId = parentNodeId;
            this.IsRoot = isRoot;
            this.IsLeaf = isLeaf;

            this.IsSelected = isSelected;
            this.IsChecked = isChecked;
        }
        #endregion

        #region Id——Guid Id

        private Guid _id;
        /// <summary>
        /// Id
        /// </summary>
        public Guid Id
        {
            get { return this._id; }
            set { this._id = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 名称——string Name
        private string _name;
        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get { return this._name; }
            set { this._name = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 上级节点Id —— Guid? ParentNodeId
        private Guid? _parentNodeId;
        /// <summary>
        /// 上级节点Id
        /// </summary>
        public Guid? ParentNodeId
        {
            get { return this._parentNodeId; }
            set { this._parentNodeId = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 是否是根级节点 —— bool IsRoot
        private bool _isRoot;
        /// <summary>
        /// 是否是根级节点
        /// </summary>
        public bool IsRoot
        {
            get { return this._isRoot; }
            set { this._isRoot = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 是否是叶子级节点 —— bool IsLeaf
        private bool _isLeaf;
        /// <summary>
        /// 是否是叶子级节点
        /// </summary>
        public bool IsLeaf
        {
            get { return this._isLeaf; }
            set { this._isLeaf = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 是否选中——bool? IsSelected
        private bool? _isSelected;
        /// <summary>
        /// 是否选中
        /// </summary>
        public bool? IsSelected
        {
            get { return this._isSelected; }
            set { this._isSelected = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 是否勾选——bool? IsChecked
        private bool? _isChecked;
        /// <summary>
        /// 是否勾选
        /// </summary>
        public bool? IsChecked
        {
            get { return this._isChecked; }
            set { this._isChecked = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 子节点——ObservableCollection<TreeNode<T>> SubNodes
        private ObservableCollection<TreeNode<T>> _subNodes;
        /// <summary>
        /// 子节点
        /// </summary>
        public ObservableCollection<TreeNode<T>> SubNodes
        {
            get { return this._subNodes; }
            set { this._subNodes = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region # 方法
        #region # 填充子元素——void FillSubNodes(List<TreeNode> treeNodes, TreeNode treeNode)
        /// <summary>
        /// 填充子元素
        /// </summary>
        /// <param name="treeNodes">树节点集合</param>
        /// <param name="treeNode">父级树节点</param>
        public static void FillSubNodes(List<TreeNode<T>> treeNodes, TreeNode<T> treeNode)
        {
            List<TreeNode<T>> subSampleClasses = treeNodes.Where(x => x.ParentNodeId == treeNode.Id).ToList();
            foreach (var subSampleClass in subSampleClasses)
            {
                if (!subSampleClass.IsLeaf)
                {
                    FillSubNodes(treeNodes, subSampleClass);
                }
            }

            treeNode.SubNodes.AddRange(subSampleClasses);
        }
        #endregion 
        #endregion
    }
}
